package com.le.tester.imooc.masterJUC.lock.deadlock;

import java.util.Random;

/**
 * 描述：模拟多人转账
 */
public class MultiTransferMoney {
    private static final int NUM_ACCOUNT = 5000;
    private static final int NUM_MONEY = 1000;
    private static final int NUM_ITERATIONS = 10000000;
    private static final int NUM_THREAD = 10;

    public static void main(String[] args) {
        Random random = new Random();
        TransferMoney.Account[] accounts = new TransferMoney.Account[NUM_ACCOUNT];
        for (int i = 0; i < accounts.length; i++) {
            accounts[i] = new TransferMoney.Account(NUM_MONEY);
        }
        class TransferThread extends Thread {

            @Override
            public void run() {
                for (int i = 0; i < NUM_ITERATIONS; i++) {
                    int fromAccount = random.nextInt(NUM_ACCOUNT);
                    int toAccount = random.nextInt(NUM_ACCOUNT);
                    int accountMoney = random.nextInt(NUM_MONEY);
                    TransferMoney.transferMoney(new TransferMoney.Account(fromAccount),
                            new TransferMoney.Account(toAccount), accountMoney);
                }
            }
        }
        for (int i = 0; i < NUM_THREAD; i++) {
            new TransferThread().start();
        }

    }
}
